package cn.example.web.module.channel.handler;

import cn.example.web.base.AbstractController;
import cn.example.web.cache.CacheSign;
import cn.example.web.cache.CacheUser;
import cn.example.web.db.bean.Channel;
import cn.example.web.db.bean.User;
import cn.example.web.module.user.ParameterConstant;
import cn.example.web.module.user.UserService;
import cn.example.web.module.user.UserVO;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 渠道处理器
 * @author SandKing
 *
 */
public abstract class AbstractChannelHandler {
	
	/** 检查登录参数 */
	public boolean checkLoginParameter(AbstractController controller) {
		boolean bool = true;
		bool &= StrUtil.isNotBlank(controller.getPara(ParameterConstant.ACCOUNT));
		bool &= StrUtil.isNotBlank(controller.getPara(ParameterConstant.CHANNEL_PACK_ID));
		bool &= StrUtil.isNotBlank(controller.getPara(ParameterConstant.DEVICE_TOKEN));
		bool &= StrUtil.isNotBlank(controller.getPara(ParameterConstant.DEVICE_NAME));
		bool &= StrUtil.isNotBlank(controller.getPara(ParameterConstant.DEVICE_ID));
		bool &= StrUtil.isNotBlank(controller.getPara(ParameterConstant.OS_NAME));
		bool &= StrUtil.isNotBlank(controller.getPara(ParameterConstant.OS_VERSION));
		bool &= StrUtil.isNotBlank(controller.getPara(ParameterConstant.CLIENT_VERSION));
		bool &= onCheckLoginParameter(controller);
		return bool;
	}

	/** 登录验证 
	 * @param channel */
	public UserVO login(AbstractController controller, Channel channel) {
		String account = controller.getPara(ParameterConstant.ACCOUNT);
		long channelId = channel.getId();
		
		UserVO userVO = CacheUser.me().getByAccountPassword(account, channelId);
		if (userVO == null) {
			User user = getUserService().findByAccountChannelId(account, channelId);
			if (user == null) {
				userVO = createUser(controller,channelId);
			}else {
				userVO = new UserVO(user);
			}	
		}
		CacheUser.me().add(userVO);

		onLogin(controller, userVO);

		// 生成签名
		String newSign = IdUtil.fastSimpleUUID();
		String oldSign = userVO.getSign();
		if (StrUtil.isNotBlank(oldSign)){
			CacheSign.me().remove(oldSign);
		}
		userVO.setSign(newSign);
		CacheSign.me().put(newSign,userVO);
		return userVO;
	}

	/**
	 * 创建用户
	 * @param controller
	 */
	private UserVO createUser(AbstractController controller,long channelId) {
		String account = controller.getPara(ParameterConstant.ACCOUNT);
		int channelPackId = controller.getParaToInt(ParameterConstant.CHANNEL_PACK_ID);
		String deviceToken = controller.getPara(ParameterConstant.DEVICE_TOKEN);
		String deviceName = controller.getPara(ParameterConstant.DEVICE_NAME);
		String deviceId = controller.getPara(ParameterConstant.DEVICE_ID);
		String osName = controller.getPara(ParameterConstant.OS_NAME);
		String osVersion = controller.getPara(ParameterConstant.OS_VERSION);
		String clientVersion = controller.getPara(ParameterConstant.CLIENT_VERSION);
		
		UserVO userVO = new UserVO();
		userVO.setAccount(account);
		userVO.setChannelId(channelId);
		userVO.setChannelPackId(channelPackId);
		userVO.setDeviceToken(deviceToken);
		userVO.setDeviceName(deviceName);
		userVO.setDeviceId(deviceId);
		userVO.setOsName(osName);
		userVO.setOsVersion(osVersion);
		userVO.setPhoneNumber("");
		userVO.setEmail("");
		userVO.setLanguage("");
		userVO.setClientVersion(clientVersion);
		
		onCreate(controller,userVO);
		
		getUserService().save(userVO);
		
		return userVO;
	}

	protected abstract UserService getUserService();
	
	protected abstract boolean onCheckLoginParameter(AbstractController controller);
	
	protected abstract UserVO onCreate(AbstractController controller,UserVO userVO);
	
	protected abstract UserVO onLogin(AbstractController controller,UserVO userVO);
}
